package com.ld.manageBook.service.impl;

import com.ld.manageBook.DAO.BorrowDAO;
import com.ld.manageBook.bean.Borrow;
import com.ld.manageBook.service.BorrowService;

import java.sql.Date;
import java.time.LocalDate;
import java.util.List;

public class BorrowImpl implements BorrowService {
	private BorrowDAO borrowDAO = new BorrowDAO();
	public int borrowBook(Borrow borrow){

			return borrowDAO.update("insert into borrow(user_name,book_id,time,number,needreturn) values(?,?,?,?,?)",borrow.getUser_name(),borrow.getBook_id(),Date.valueOf(LocalDate.now()),borrow.getNumber(),borrow.getNumber());

	}

	@Override
	public int getNeedReturnById(Integer id) {
		Object i = borrowDAO.queryScalar("select needreturn from borrow where id = ?", id);
		if(i == null) {
			return 0;
		}else {
			return (int)i;
		}

	}

	@Override
	public List<Borrow> getBorrowBookByUserName(String userName) {
		return borrowDAO.queryMulti("select * from borrow where user_name = ? ",Borrow.class,userName);
	}

	@Override
	public Borrow getBorrowById(Integer id) {
		return borrowDAO.querySingle("select * from borrow where id = ?",Borrow.class,id);
	}

	@Override
	public int updateNeedReturnById(Integer id, Integer returnNumber) {
		int needReturnById = getNeedReturnById(id);
		return borrowDAO.update("update borrow set needreturn = ? where id = ?",needReturnById - returnNumber,id);
	}
}
